﻿namespace StudyCompanion.Commons.Cache;

public static class RedisLock
{
    /// <summary>
    ///     分布式锁
    /// </summary>
    /// <param name="key">锁key</param>
    /// <param name="lockExpirySeconds">锁自动超时时间(秒)</param>
    /// <param name="waitLockSeconds">等待锁时间(秒)</param>
    /// <returns></returns>
    public static bool Lock(string key, int lockExpirySeconds = 10, double waitLockSeconds = 0)
    {
        //间隔等待50毫秒
        var waitIntervalMs = 50;

        var lockKey = "lock:" + key;

        var begin = DateTime.Now;
        while (true)
        {
            //循环获取取锁
            if (RedisHelper.SetNx(lockKey, true))
            {
                // Console.WriteLine($"订单锁：{lockKey},时间：{DateTime.Now}");
                //设置锁的过期时间
                RedisHelper.Expire(lockKey, lockExpirySeconds);
                return true;
            }

            //不等待锁则返回
            if (waitLockSeconds == 0) break;

            //超过等待时间，则不再等待
            if ((DateTime.Now - begin).TotalSeconds >= waitLockSeconds) break;

            Thread.Sleep(waitIntervalMs);
        }

        return false;
    }

    /// <summary>
    ///     删除锁 执行完代码以后调用释放锁
    /// </summary>
    /// <param name="key"></param>
    public static void DelLock(string key)
    {
        var lockKey = "lock:" + key;
        // Console.WriteLine($"订单解锁：{lockKey}");
        RedisHelper.Del(lockKey);
    }
}